using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;
using BriansClimb.DrawableGameObjects.Buttons;
using BriansClimb.DrawableGameObjects.GameScenes;
using BriansClimb.DrawableGameObjects.ClimbingHolds;

namespace BriansClimb.GameObjects
{
    /// <summary>
    /// This is a game component that implements the toggle button functionality.
    /// </summary>
    public class ButtonManager : Microsoft.Xna.Framework.GameComponent
    {
        protected Game game;
        protected GameScene activeScene;
        protected SceneManager sceneManager;
        protected WallCustomizeScene wallCustomizeScene;
        protected ClimbingHold climbingHold;

        protected MouseState mouseState;

        protected bool clicked;
        protected bool pressed;
        protected int index;

        /// <summary>
        /// Default Constructor
        /// </summary>
        /// <param name="game">Main Game Object</param>
        /// <param name="sceneManager">SceneManager Object</param>
        /// <param name="wallCustomizeScene">WallCustomizeScene Object</param>
        public ButtonManager(Game game, SceneManager sceneManager, WallCustomizeScene wallCustomizeScene)
            : base(game)
        {
            this.game = game;
            this.sceneManager = sceneManager;
            this.wallCustomizeScene = wallCustomizeScene;
        }

        /// <summary>
        /// Checks for a left mouse button press
        /// </summary>
        private void CheckButtonPress()
        {
            mouseState = Mouse.GetState();
            pressed = false;

            if (mouseState.LeftButton == ButtonState.Pressed)
                pressed = true;
        }

        /// <summary>
        /// Handles the on/off toggle relationship between the different toggle buttons.
        /// </summary>
        private void ManageToggledStates(GameTime gameTime)
        {
            List<ToggleButton> currentToggleButtonList = wallCustomizeScene.ToggleButtonList;

            /*
             * allows the toggling on/off of the buttons when other toggle buttons are on/off.
             * this is done by checking the most recent toggled button and un-toggling all
             * other buttons.
             */
            foreach (ToggleButton tb in currentToggleButtonList)
            {
                if (gameTime == tb.Time)
                    foreach (ToggleButton tb2 in currentToggleButtonList)
                    {
                        if (tb != tb2)
                            tb2.Toggled = false;
                    }
                tb.Time = null;
            }
        }

        /// <summary>
        /// Sets the climbing hold's type based on which toggle button is activated.
        /// </summary>
        private void ActivateToggleButton()
        {
            /*
             * checks which toggle button is activated then sends the string name of climbing hold attached
             * to the activated button
             */
            if (wallCustomizeScene.RestButton.Toggled)
                wallCustomizeScene.wallManager.ClimbingHoldType = "rest";

            if (wallCustomizeScene.JugButton.Toggled)
                wallCustomizeScene.wallManager.ClimbingHoldType = "jug";

            if (wallCustomizeScene.FootHoldButton.Toggled)
                wallCustomizeScene.wallManager.ClimbingHoldType = "foot";

            if (wallCustomizeScene.CrimpHoldButton.Toggled)
                wallCustomizeScene.wallManager.ClimbingHoldType = "crimp";

            if (wallCustomizeScene.SlopeHoldButton.Toggled)
                wallCustomizeScene.wallManager.ClimbingHoldType = "slope";

            //if the button is not toggled, then hold type is set to null, thereby not drawing anything
            if ((!wallCustomizeScene.RestButton.Toggled) && (!wallCustomizeScene.CrimpHoldButton.Toggled) && 
                (!wallCustomizeScene.JugButton.Toggled) && (!wallCustomizeScene.FootHoldButton.Toggled) && 
                (!wallCustomizeScene.SlopeHoldButton.Toggled))
                wallCustomizeScene.wallManager.ClimbingHoldType = null;
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            activeScene = sceneManager.ActiveScene;

            //Options for wallcustomize scene's toggle & left-click buttons
            if (activeScene == wallCustomizeScene)
            {
                //Toggle button functionality
                ManageToggledStates(gameTime);
                ActivateToggleButton();
            }

            base.Update(gameTime);
        }
    }
}